/*
 * Copyright (c) 2018-2999 深圳市阿毛科技有限公司 All rights reserved.
 *
 * https://www.lamyz.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.lam.mall.service.impl;

import java.util.List;
import java.util.Objects;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lam.common.utils.StringUtils;
import com.lam.mall.app.vo.UserCollectionVo;
import com.lam.mall.entity.UserCollection;
import com.lam.mall.mapper.UserCollectionMapper;
import com.lam.mall.param.UserCollectionParam;
import com.lam.mall.service.UserCollectionService;

/**
 * 用户收藏表
 *
 */
@Service
public class UserCollectionServiceImpl implements UserCollectionService {

    @Autowired
    private UserCollectionMapper userCollectionMapper;

    @Override
    public List<UserCollectionVo> getPageByUserId(String userId) {
        return userCollectionMapper.getPageByUserId(userId);
    }
    
    @Override
    public boolean checkExist(String userId, String prodId) {
    	return userCollectionMapper.checkExist(userId, prodId)!= null;
    }
    
    @Override
    public int delete(String userId, String prodId) {
    	QueryWrapper<UserCollection> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("prod_id", prodId);
        queryWrapper.eq("user_id", userId);
        return userCollectionMapper.delete(queryWrapper);
    }
    
    @Override
    public Long selectCount(String userId) {
    	QueryWrapper<UserCollection> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        return userCollectionMapper.selectCount(queryWrapper);
    }
    
    /**
     * 查询用户收藏
     * 
     * @param id ID
     * @return 单个对象
     */
    @Override
    @Transactional(readOnly = true)
    public UserCollection selectById(String id) {

        return userCollectionMapper.selectById(id);
    }

    /**
     * 查询用户收藏列表
     * 
     * @param param
     * @return 
     */
    @Override
    @Transactional(readOnly = true)
    public List<UserCollection> selectList(UserCollectionParam param) {
    
        QueryWrapper<UserCollection> queryWrapper = new QueryWrapper<>();
        
        queryWrapper.eq(Objects.nonNull(param.getProdId()), "prod_id", param.getProdId());
        queryWrapper.eq(StringUtils.isNotBlank(param.getUserId()), "user_id", param.getUserId());

        return userCollectionMapper.selectList(queryWrapper);
    }

    /**
     * 新增用户收藏
     * 
     * @param userCollection 
     * @return 
     */
    @Override
    @Transactional
    public int insert(UserCollection userCollection) {
    
        return userCollectionMapper.insert(userCollection);
    }

    /**
     * 修改用户收藏
     * 
     * @param userCollection 
     * @return 
     */
    @Override
    @Transactional
    public int update(UserCollection userCollection) {
    
        return userCollectionMapper.updateById(userCollection);
    }

    /**
     * 删除用户收藏信息
     * 
     * @param idStr ID，多个用逗号分隔
     * @return 
     */
    @Override
    @Transactional
    public int deleteById(String idStr) {
    
        if(StringUtils.isBlank(idStr)) {
            return 0;
        }
        return userCollectionMapper.deleteBatchIds(StringUtils.str2List(idStr));
    }
}
